<?php
/**
* @version $Id: Heriniaina Eugene  04/05/2006 17.23 $
* @package mod_newsboxes
* @copyright Copyright (C) 2006 serasera.org
* @comment This modules shows news boxes like in CNN.com
* URL : http://hery.blaogy.org/2007/03/22/joomla-newsbox-module/
*/

// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$option = trim( mosGetParam( $_REQUEST, 'option', null ) );
$task = trim( mosGetParam( $_REQUEST, 'task', null ) );
$id = intval( mosGetParam( $_REQUEST, 'id', null ) );

global $mosConfig_cachepath, $_MAMBOTS;


$noauth 			= !$mainframe->getCfg( 'shownoauth' );
$nbx_sectionid =  intval($params->def('nbx_sectionid', 0));
$nbx_catid =  intval($params->def('nbx_catid', 0));
$nbx_rows =  intval($params->def('nbx_rows', 2));
$nbx_columns =  intval($params->def('nbx_columns', 1));
$nbx_showtitles =  intval($params->def('nbx_showtitles', 1));
$nbx_showintro =  intval($params->def('nbx_showintro', 1));
$nbx_introlength =  intval($params->def('nbx_introlength', 300));
$nbx_introformat =  $params->def('nbx_introformat', 'Text');
$nbx_readmore =  intval($params->def('nbx_readmore', 1));
$nbx_titlelink =  intval($params->def('nbx_titlelink', 1));
$nbx_showimage =  intval($params->def('nbx_showimage', 1));
$nbx_ordering =  intval($params->def('nbx_ordering', 1));

if($nbx_sectionid == 0 && $nbx_catid == 0) {
	echo "This module is not yet configured... Please configure it or remove it.";

	
} else {

	
	$query = "SELECT a.id, a.introtext, a.images, a.fulltext, a.attribs, a.title, a.state"
	."\n FROM #__content AS a"
	."\n INNER JOIN #__categories AS cc ON cc.id = a.catid"
	."\n INNER JOIN #__sections AS s ON s.id = a.sectionid"
	."\n WHERE a.state = 1"
	. ( $noauth ? "\n AND a.access <= " . (int) $my->gid . " AND cc.access <= " . (int) $my->gid . " AND s.access <= " . (int) $my->gid : '' )
	. ($nbx_sectionid > 0  ? "\n AND a.sectionid = " . $nbx_sectionid : "")
	. ($nbx_catid > 0  ? "\n AND a.catid = " . $nbx_catid : "")
	."\n AND cc.published = 1"
	."\n AND s.published = 1"
	. ($nbx_ordering == 1 ? "\n ORDER BY a.ordering " : "\n ORDER BY a.created DESC " )
	;

	$database->setQuery( $query, 0, $nbx_rows );
	if($rows = $database->loadObjectList()) {
		echo ($nbx_columns > 1) ? "<table><tr><td valign=\"top\">" : "";
		
		$col = 0;

		if($nbx_showintro == 0) {
			echo "<ul class=\"" . $params->get( 'moduleclass_sfx') . "box\">";
		}

		foreach($rows as $row) {
			if($nbx_showintro != 0) {
				echo "<div class=\"" . $params->get( 'moduleclass_sfx') . "box\">";
			}
			$col++;

			$row->text = $row->introtext;

			$_MAMBOTS->loadBotGroup( 'content' );
			$results = $_MAMBOTS->trigger( 'onPrepareContent', array( &$row, &$params, $page ), true );

			
			if($nbx_showtitles != 0) {
				if($nbx_showintro == 0) {
					echo "<li class=\"title\">";
				} else {
					echo  "<div class=\"title\">" ;
				}
				if($nbx_titlelink != 0) {
					echo "<a href=\"" . sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id=' . $row->id . $row->Itemid_link ) . "\">";
				}	
				echo $row->title ;
				if($nbx_titlelink != 0) {
					echo "</a>";
				}	
				
				if($nbx_showintro == 0) {
					echo "</li>";
				} else {
					echo  "</div>" ;
				}
				
			}
			if($nbx_showintro != 0) {
				echo "<div class=\"text\">";

				if($nbx_showimage > 0 && $row->images != "") {

					$img_str = $row->images;
					// split on \n the images fields into an array
					$img_str = explode( "\n", $row->images );

					$img = trim( $img_str[0] );

						// split on pipe the attributes of the image
					if ( $img ) {
						$attrib = explode( '|', trim( $img ) );
						// $attrib[0] image name and path from /images/stories

						// $attrib[1] alignment
						if ( !isset($attrib[1]) || !$attrib[1] ) {
							$attrib[1] = '';
						}

						// $attrib[2] alt & title
						if ( !isset($attrib[2]) || !$attrib[2] ) {
							$attrib[2] = 'Image';
						} else {
							$attrib[2] = htmlspecialchars( $attrib[2] );
						}


						// assemble the <image> tag
						echo '<img src="'. $mosConfig_live_site .'/images/stories/'. $attrib[0] .'"'. $size;
						echo ' hspace="6" alt="'. $attrib[2] .'" title="'. $attrib[2] .'" border="'. $border .'" />';

					}

				}
				if($nbx_introformat == 'Text') {
					if($nbx_introlength > 0) {
						echo (strlen(strip_tags($row->text)) > $nbx_introlength) ? substr(strip_tags($row->text), 0, $nbx_introlength) . "... " : strip_tags($row->text);
						
					} else {
						echo strip_tags($row->text);
					}
				} else {
					echo ampReplace( $row->text );
				}
				echo "</div>";
			}
			echo ($nbx_readmore != 0 ) ? "<div class=\"readmore\"><a href=\"" . sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id=' . $row->id . $row->Itemid_link ) . "\" class=\"readon" . $params->get( 'moduleclass_sfx') . "\">" . _READ_MORE . "</a></div>": "";
			if($nbx_showintro != 0) {
				echo "</div>";
			}
			echo ($nbx_columns > 1 && (floor($nbx_rows / $nbx_columns)  == $col)) ? "</td><td valign=\"top\">" : "";
		}
		if($nbx_showintro == 0) {
			echo "</ul>";
		}

		echo ($nbx_columns > 1) ? "</td></tr></table>" : "";

	}
}


?>